package com.cg;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

/*
 * 手写线程池
 */
public class MyThreadPool {

    private List<Runnable> runnableList;

    private BlockingDeque<Runnable> blockingDeque;

    public MyThreadPool(int maxThreadCount, int dequeSize) {
        blockingDeque = new LinkedBlockingDeque<>(dequeSize);
        runnableList = new ArrayList<>(maxThreadCount);
        for (int i = 0; i < maxThreadCount; i++) {
            new WorkThread().start();
        }
    }

    class WorkThread extends Thread {

        @Override
        public void run() {
            while (true) {
                Runnable runnable = blockingDeque.poll();
                if (runnable != null) {
                    runnable.run();
                }
            }
        }
    }

    public void execute(Runnable runnable) {
        blockingDeque.offer(runnable);
    }

    public static void main(String[] args) throws Exception {
//        MyThreadPool myThreadPool = new MyThreadPool(2, 2);
//        for (int i = 0; i < 10; i++) {
//            int finalI = i;
//            myThreadPool.execute(() -> System.out.println(Thread.currentThread().getName() + " , " + finalI));
//        }
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "123");
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "234");
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "345");
        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(future1, future, future2);
        System.out.println(objectCompletableFuture.get());
    }
}
